<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\service\AliyunSms;
use App\Util\Tool;
use Illuminate\Support\Facades\DB;

class SmsLog extends Model
{
    /*
     * 定义的查询表名
     */
    protected $table = 'sms_logs';

    /*
     * 查询字段
     */
    protected $fillable = [
        'type',
        'mobile',
        'code',
        'checked',
        'status',
        'reason',
        'remark',
        'ip',
        'operator_id',
        'result_msg',
        'created_at',
        'updated_at',
    ];

    //类型(0:短信验证码,1:语音验证码,2:短信消息通知)
    const TYPE_CODE = 0;
    const TYPE_VOICE = 1;
    const TYPE_MESSAGE = 2;

    //是否验证(0:未验证,1:已验证)
    const CHECKED_UNVERIFIED = 0;
    const CHECKED_VERIFIED = 1;

    //状态(0:未发送,1:已发送,2:发送失败)
    const STATUS_NO_SEND = 0;
    const STATUS_SEND = 1;
    const STATUS_FAIL = 2;

    //短信发送间隔时间，默认60秒
    const SEND_INTERVAL_TIME = 60;

    /**
     * 检测短信验证码
     */
    protected function CheckCode($mobile, $code)
    {
        if (!$mobile) {
            throw new \Exception('手机号不能为空');
        }
        if (!checkMobile($mobile)) {
            throw new \Exception('手机号不正确');
        }
        if (!$code) {
            throw new \Exception('验证码不能为空');
        }
        $sms_log = $this->where([
            ['type', self::TYPE_CODE],
            ['mobile', $mobile],
            ['status', self::STATUS_SEND],
            ['checked', self::CHECKED_UNVERIFIED],
        ])->orderBy('created_at', 'desc')->first();
        if (!$sms_log) {
            throw new \Exception('验证码不存在,请重新获取');
        }
        if ($code != $sms_log->code) {
            throw new \Exception('验证码错误');
        }
        $sms_log->checked = self::CHECKED_VERIFIED;
        $sms_log->save();
        return true;
    }

    /**
     * 检测短信频率
     */
    public function checkRate($mobile)
    {
        return $this->where([
            ['mobile', $mobile],
            ['checked', self::CHECKED_UNVERIFIED],
            ['status', self::STATUS_SEND],
        ])->orderBy('created_at', 'desc')->first();
    }

    /**
     *更新验证码以使用
     */
    public function updateCode($mobile, $code)
    {
        return $this->where([
            ['mobile', $mobile],
            ['status', self::STATUS_SEND],
            ['checked', self::CHECKED_UNVERIFIED],
            ['code', $code],
        ])->update(['checked' => self::CHECKED_VERIFIED]);
    }

    /**
     * 写入短信发送记录
     */
    public function createLog(int $mobile, int $code, int $status, string $result)
    {
        $sms_log = $this->create([
            'type' => self::TYPE_CODE,
            'mobile' => $mobile,
            'code' => $code,
            'checked' => self::CHECKED_UNVERIFIED,
            'result_msg' => $result,
            'status' => $status,
            'ip' => Tool::getRealIp(),
        ]);
        return $sms_log->save();
    }

    /**
     * @param int $mobile
     * @param int $code
     * @param int $status
     * @param string $result
     * @return mixed
     * 工作室短信验证
     */
    public function createWorklog(int $mobile, int $code, int $status, string $result)
    {
        $sms_log = $this->create([
            'type' => self::TYPE_MESSAGE,
            'mobile' => $mobile,
            'code' => $code,
            'checked' => self::CHECKED_VERIFIED,
            'result_msg' => $result,
            'status' => $status,
            'ip' => Tool::getRealIp(),
        ]);
        return $sms_log->save();
    }
}
